<template>
  <mytitle title="登录系统">
     <template #left>
          <el-link type="primary">注册</el-link>
     </template>
     <template #right>
          <el-link type="primary">了解系统</el-link>
     </template>
  </mytitle>
 
 <div class="login-container">
  <el-form
    style="width:80%"
    status-icon
    label-width="auto"
  >
    <el-form-item label="用户名">
      <el-input type="text" v-model="user.loginName" />
    </el-form-item>
    <el-form-item label="密码" >
      <el-input
        type="password"
        autocomplete="off"
        v-model="user.loginPwd"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="login">
        登录
      </el-button>
      <el-button>注册</el-button>
    </el-form-item>
  </el-form>
  </div>
</template>

<script  setup>
   import { ref,reactive} from 'vue'
   import axios from '../axios/index.js'
   import {userStore} from '../store/UserStore.js'
   import {useRouter} from 'vue-router'
   import qs from 'qs'
   import mytitle from '../components/Title.vue'
   const store=userStore();
   const router=useRouter();
   const user=reactive({
     loginName:"",
     loginPwd:""
   })
  
   //async function  login(){
    
    const login=async()=>{
      let d=await axios.post("/login",qs.stringify(user))
      if(d.data.code==2000){
          let user=d.data.data;
          store.saveUser(user);
          alert("登录成功");
          router.push("/list")
      }else{
          alert(d.data.msg)
      }
   }

   /*function login(){
     //是发送数据给后端进行验证
     axios
       //.post("/login",`loginName=${user.loginName}&loginPwd=${user.loginPwd}`)
       .post("/login",qs.stringify(user))
       .then((d)=>{
         alert(JSON.stringify(d.data));
         //放到pinia里头传递给userInfo显示
         if(d.data.code==2000){
              let user=d.data.data;
              store.saveUser(user);
              alert("登录成功");
              router.push("/list")
         }else{
             alert(d.data.msg)
         }
      })
   
   }*/
  
</script>

<style scoped>
  .login-container{
     display: flex;
     justify-content: center;
     align-items: center;
     width:100%;
  }
  h2{text-align: center;}
  .login-btn{text-align: center;}
</style>
